<template name="ad_setting">
  <view class="settings-container">
    <!-- 顶部标题 -->
    <view class="settings-header">
      <view class="header-content">
        <view class="header-left">
          <text class="page-title">积分设置与分类管理</text>
          <text class="page-desc">配置发布规则和内容分类</text>
        </view>
        <view class="refresh-page-btn" @click="refreshPage">
          <text class="refresh-icon">🔄</text>
          <text class="refresh-text">刷新页面</text>
        </view>
      </view>
    </view>

    <view class="flex" style="gap: 20px;">
      <!-- 积分设置卡片 -->
      <view class="settings-card">
        <view class="card-header">
          <text class="card-title">积分设置</text>
          <text class="card-subtitle">配置发布和置顶所需的积分</text>
        </view>
        
        <view class="points-form">
          <view class="form-item">
            <view class="form-label">
              <text class="label-text">发布一条信息流</text>
              <text class="label-desc">用户每次发布内容消耗的积分</text>
            </view>
            <view class="form-control">
              <input 
                class="points-input" 
                type="number" 
                v-model="pointsConfig.publishPoints" 
                placeholder="请输入积分"
              />
              <text class="points-unit">积分</text>
            </view>
          </view>
      
          <view class="form-item">
            <view class="form-label">
              <text class="label-text">发布一条置顶一个月信息流</text>
              <text class="label-desc">内容置顶30天消耗的积分</text>
            </view>
            <view class="form-control">
              <input 
                class="points-input" 
                type="number" 
                v-model="pointsConfig.stickyPoints" 
                placeholder="请输入积分"
              />
              <text class="points-unit">积分</text>
            </view>
          </view>
      
	  
		  <view class="form-item">
			<view class="form-label">
			  <text class="label-text">发布一条名片</text>
			  <text class="label-desc">用户每次发布内容消耗的积分</text>
			</view>
			<view class="form-control">
			  <input 
				class="points-input" 
				type="number" 
				v-model="pointsConfig.card_publishPoints" 
				placeholder="请输入积分"
			  />
			  <text class="points-unit">积分</text>
			</view>
		  </view>
		  
         <view class="form-item">
            <view class="form-label">
              <text class="label-text">发布一条置顶一个月名片</text>
              <text class="label-desc">内容置顶30天消耗的积分</text>
            </view>
            <view class="form-control">
              <input 
                class="points-input" 
                type="number" 
                v-model="pointsConfig.card_stickyPoints" 
                placeholder="请输入积分"
              />
              <text class="points-unit">积分</text>
            </view>
          </view>
		  
		  <view class="form-item">
			<view class="form-label">
			  <text class="label-text">发布一条群聊</text>
			  <text class="label-desc">用户每次发布内容消耗的积分</text>
			</view>
			<view class="form-control">
			  <input 
				class="points-input" 
				type="number" 
				v-model="pointsConfig.group_publishPoints" 
				placeholder="请输入积分"
			  />
			  <text class="points-unit">积分</text>
			</view>
		  </view>
		  
         <view class="form-item">
            <view class="form-label">
              <text class="label-text">发布一条置顶一个月群聊</text>
              <text class="label-desc">内容置顶30天消耗的积分</text>
            </view>
            <view class="form-control">
              <input 
                class="points-input" 
                type="number" 
                v-model="pointsConfig.group_stickyPoints" 
                placeholder="请输入积分"
              />
              <text class="points-unit">积分</text>
            </view>
          </view>
		  
		  
		  <view class="form-item">
		     <view class="form-label">
		       <text class="label-text">签到赠送积分</text>
		       <text class="label-desc">用户每天均可进行签到</text>
		     </view>
		     <view class="form-control">
		       <input 
		         class="points-input" 
		         type="number" 
		         v-model="pointsConfig.signInReward" 
		         placeholder="请输入积分"
		       />
		       <text class="points-unit">积分</text>
		     </view>
		   </view>
		  
		  
		  <view class="form-item">
		     <view class="form-label">
		       <text class="label-text">邀请好友赠送积分</text>
		       <text class="label-desc">用户可邀请好友通过小程序进行注册审核通过后可获得的积分,H5注册无效</text>
		     </view>
		     <view class="form-control">
		       <input 
		         class="points-input" 
		         type="number" 
		         v-model="pointsConfig.shareReward" 
		         placeholder="请输入积分"
		       />
		       <text class="points-unit">积分</text>
		     </view>
		   </view>
          <view class="form-actions">
            <button class="save-btn" @tap="savePointsConfig">保存积分设置</button>
          </view>
        </view>
      </view>
      
      <!-- 分类管理卡片 -->
      <view class="settings-card">
        <view class="card-header">
          <text class="card-title">分类管理</text>
          <text class="card-subtitle">管理内容分类，支持添加、编辑、删除</text>
        </view>
      
        <!-- 添加分类表单 -->
        <view class="category-form">
          <view class="form-row">
            <view class="input-group">
              <text class="input-label">分类名称</text>
              <input 
                class="category-input" 
                v-model="newCategory.name" 
                placeholder="请输入分类名称"
                maxlength="10"
              />
            </view>
            <view class="input-group">
              <text class="input-label">排序权重</text>
              <input 
                class="category-input" 
                type="number" 
                v-model="newCategory.sort" 
                placeholder="数字越小越靠前"
              />
            </view>

            <button class="add-btn" @tap="addCategory">添加分类</button>
          </view>
        </view>
      
        <!-- 分类列表 -->
        <view class="categories-list">
          <view class="list-header">
            <text class="header-col name">分类名称</text>
            <text class="header-col sort">排序</text>
            <text class="header-col actions">操作</text>
          </view>
      
          <view class="list-body">
            <view 
              v-for="(category, index) in categories" 
              :key="category.id"
              class="category-item"
              :class="{editing: category.editing}"
            >
              <!-- 查看模式 -->
              <view class="item-view" v-if="!category.editing">
                <text class="item-col name">{{ category.name }}</text>
                <text class="item-col sort">{{ category.sort }}</text>
          
                <view class="item-col actions">
                  <view class="action-btn edit" @tap="startEditCategory(index)">编辑</view>
                  <view class="action-btn delete" @tap="showDeleteCategoryConfirm(index)">删除</view>
                </view>
              </view>
      
              <!-- 编辑模式 -->
              <view class="item-edit" v-else>
                <input 
                  class="edit-input name" 
                  v-model="category.name" 
                  placeholder="分类名称"
                />
                <input 
                  class="edit-input sort" 
                  type="number" 
                  v-model="category.sort" 
                  placeholder="排序"
                />
    
                <view class="edit-actions">
                  <view class="action-btn save" @tap="saveCategory(index)">保存</view>
                  <view class="action-btn cancel" @tap="cancelEdit(index)">取消</view>
                </view>
              </view>
            </view>
          </view>
      
          <!-- 空状态 -->
          <view class="empty-categories" v-if="categories.length === 0">
            <text class="empty-icon">📁</text>
            <text class="empty-text">暂无分类，请添加分类</text>
          </view>
        </view>
      </view>
        
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 积分配置
      pointsConfig: {
        publishPoints: 0,
        stickyPoints: 0,
        card_publishPoints: 0,
        card_stickyPoints: 0,
		
		group_publishPoints: 0,
		group_stickyPoints: 0,
		
        signInReward: 0,
        shareReward: 0,
      },
      // 新分类数据
      newCategory: {
        name: '',
        sort: 0,
      },
      // 分类列表
      categories: []
    }
  },

  created() {
    this.loadPointsConfig();
    this.loadCategories();
  },
  methods: {
    // 刷新页面数据
    refreshPage() {
      this.loadPointsConfig();
      this.loadCategories();
      this.$common.toast('页面数据已刷新');
    },

    // 加载积分配置
    loadPointsConfig() {
      // 模拟从服务器加载数据
      this.$common.get_request(
        {
          type: 'ad_getPointsConfig',
        },
        (res) => {
          this.pointsConfig = res.data;
        },
        () => {
          // 失败使用默认值
        },
        false
      );
    },

    // 保存积分配置
    savePointsConfig() {

      this.$common.get_request(
        {
          type: 'ad_savePointsConfig',
          publishPoints: this.pointsConfig.publishPoints,
          stickyPoints: this.pointsConfig.stickyPoints,
		  card_publishPoints: this.pointsConfig.card_publishPoints,
		  card_stickyPoints: this.pointsConfig.card_stickyPoints,
		  signInReward: this.pointsConfig.signInReward,
		  shareReward: this.pointsConfig.shareReward,
		  
		  group_publishPoints: this.pointsConfig.group_publishPoints,
		  group_stickyPoints: this.pointsConfig.group_stickyPoints,
		  
        },
        (res) => {
          this.$common.showModal('成功', '积分设置已保存');
        },
        () => {},
        true
      );
    },

    // 加载分类列表
    loadCategories() {
      this.$common.get_request(
        {
          type: 'ad_GetCategory',
        },
        (res) => {
          this.categories = res.list.map(item => ({
            ...item,
            editing: false,//是否查看
          }));
        },
        () => {
          // 失败使用默认数据
        },
        false
      );
    },


    // 添加分类
    addCategory() {
      if (!this.newCategory.name.trim()) {
        this.$common.showModal('提示', '请输入分类名称');
        return;
      }

      const newCat = {
        name: this.newCategory.name.trim(),
        sort: parseInt(this.newCategory.sort) || 0,
      };

      this.$common.get_request(
        {
          type: 'ad_SetCategory',
          ...newCat
        },
        (res) => {
          this.categories.push({
            id: res.id,
            ...newCat,
            editing: false,
          });
          
          // 重置表单
          this.newCategory = {
            name: '',
            sort: 0,
          };
          
          this.$common.showModal('成功', '分类添加成功');
        },
        () => {},
        true
      );
    },

    // 开始编辑分类
    startEditCategory(index) {
      // 先取消其他正在编辑的项目
      this.categories.forEach((cat, i) => {
        if (i !== index) {
          cat.editing = false;
        }
      });
      
      this.categories[index].editing = true;

    },



    // 保存分类编辑
    saveCategory(index) {
      const category = this.categories[index];
      
      if (!category.name.trim()) {
        this.$common.showModal('提示', '请输入分类名称');
        return;
      }

      const updateData = {
        id: category.id,
        name: category.name.trim(),
        sort: parseInt(category.sort) || 0,
      };

      this.$common.get_request(
        {
          type: 'ad_UpdateCategory',
          ...updateData
        },
        (res) => {
          // 更新本地数据
          this.categories[index].name = updateData.name;
          this.categories[index].sort = updateData.sort;
          this.categories[index].editing = false;
          this.$common.showModal('成功', '分类更新成功');
        },
        () => {},
        true
      );
    },

    // 取消编辑
    cancelEdit(index) {
      this.categories[index].editing = false;
    },

    // 显示删除确认
    showDeleteCategoryConfirm(index) {
      const category = this.categories[index];
      uni.showModal({
        title: '删除确认',
        content: `确定要删除分类"${category.name}"吗？此操作不可恢复。`,
        confirmColor: '#ff4444',
        success: (res) => {
          if (res.confirm) {
            this.deleteCategory(index);
          }
        }
      });
    },

    // 删除分类
    deleteCategory(index) {
      const categoryId = this.categories[index].id;
      
      this.$common.get_request(
        {
          type: 'ad_DeleteCategory',
          id: categoryId
        },
        (res) => {
          this.categories.splice(index, 1);
          this.$common.showModal('成功', '分类删除成功');
        },
        () => {},
        true
      );
    }
  }
}
</script>

<style scoped>
.settings-container {
  height: 100%;
  background: #f8f9fa;
  padding: 24px;
  margin: 0 auto;
  width: 100%;
      overflow: auto;
}

/* 页面头部 */
.settings-header {
  margin-bottom: 32px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.header-left {
  flex: 1;
}

.page-title {
  display: block;
  font-size: 28px;
  font-weight: 700;
  color: #1a1a1a;
  margin-bottom: 8px;
}

.page-desc {
  font-size: 14px;
  color: #666;
}

.refresh-page-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: #1890ff;
  color: white;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
  margin: auto;
}

.refresh-page-btn:hover {
  background: #40a9ff;
  transform: translateY(-1px);
}

.refresh-icon {
  font-size: 16px;
}

.refresh-text {
  font-size: 14px;
}

/* 设置卡片 */
.settings-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  border: 1px solid #e1e5e9;
}

.card-header {
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f0f0;
}

.card-title {
  display: block;
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 4px;
}

.card-subtitle {
  font-size: 14px;
  color: #666;
}

/* 积分表单 */
.points-form {
  max-width: 800px;
}

.form-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
  border-bottom: 1px solid #f5f5f5;
}

.form-item:last-child {
  border-bottom: none;
}

.form-label {
  flex: 1;
}

.label-text {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin-bottom: 4px;
}

.label-desc {
  font-size: 12px;
  color: #999;
}

.form-control {
  display: flex;
  align-items: center;
  gap: 12px;
}

.points-input {
  width: 120px;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  text-align: right;
}

.points-input:focus {
  border-color: #1890ff;
  outline: none;
}

.points-unit {
  font-size: 14px;
  color: #666;
  min-width: 40px;
}

.form-actions {
  margin-top: 24px;
  text-align: right;
}

.save-btn {
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 6px;
  padding: 10px 24px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
}

.save-btn:hover {
  background: #40a9ff;
}

/* 分类表单 */
.category-form {
  background: #fafbfc;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 24px;
  border: 1px solid #e1e5e9;
}

.form-row {
  display: flex;
  gap: 16px;
  align-items: flex-end;
}

.input-group {
  flex: 1;
}

.input-label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: #666;
  margin-bottom: 6px;
}

.category-input {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
}

.category-input:focus {
  border-color: #1890ff;
  outline: none;
}

.switch-group {
  padding: 8px 0;
}

.status-switch {
  display: inline-flex;
  align-items: center;
  padding: 6px 12px;
  border-radius: 20px;
  background: #f5f5f5;
  color: #999;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid #e8e8e8;
}

.status-switch.active {
  background: #f6ffed;
  color: #52c41a;
  border-color: #b7eb8f;
}

.switch-text {
  font-size: 12px;
}

.add-btn {
  background: #52c41a;
  color: white;
  border: none;
  border-radius: 6px;
  padding: 8px 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
}

.add-btn:hover {
  background: #73d13d;
}

/* 分类列表 */
.categories-list {
  border: 1px solid #e1e5e9;
  border-radius: 8px;
  overflow: hidden;
}

.list-header {
  display: grid;
  grid-template-columns: 1fr 100px 100px 200px;
  background: #fafbfc;
  padding: 12px 16px;
  border-bottom: 1px solid #e1e5e9;
}

.header-col {
  font-size: 12px;
  font-weight: 600;
  color: #666;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.list-body {
  background: white;
}

.category-item {
  border-bottom: 1px solid #f0f0f0;
}

.category-item:last-child {
  border-bottom: none;
}

.category-item.editing {
  background: #f6ffed;
}

/* 查看模式 */
.item-view {
  display: grid;
  grid-template-columns: 1fr 100px 100px 200px;
  padding: 16px;
  align-items: center;
}

.item-col {
  font-size: 14px;
  color: #333;
}

.item-col.name {
  font-weight: 500;
}

.status-badge {
  display: inline-flex;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  background: #fff2f0;
  color: #ff4d4f;
  border: 1px solid #ffccc7;
}

.status-badge.active {
  background: #f6ffed;
  color: #52c41a;
  border-color: #b7eb8f;
}

.actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid transparent;
}

.action-btn.edit {
  background: #e6f7ff;
  color: #1890ff;
  border-color: #91d5ff;
}

.action-btn.edit:hover {
  background: #bae7ff;
}

.action-btn.delete {
  background: #fff2f0;
  color: #ff4d4f;
  border-color: #ffccc7;
}

.action-btn.delete:hover {
  background: #ffccc7;
}

.action-btn.save {
  background: #52c41a;
  color: white;
  border-color: #52c41a;
}

.action-btn.save:hover {
  background: #73d13d;
}

.action-btn.cancel {
  background: #f5f5f5;
  color: #666;
  border-color: #d9d9d9;
}

.action-btn.cancel:hover {
  background: #e8e8e8;
}

/* 编辑模式 */
.item-edit {
  display: grid;
  grid-template-columns: 1fr 100px 100px 200px;
  padding: 16px;
  align-items: center;
  gap: 8px;
}

.edit-input {
  padding: 6px 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.edit-input:focus {
  border-color: #1890ff;
  outline: none;
}

.edit-status {
  display: flex;
  align-items: center;
  height: 100%;
}

.edit-actions {
  display: flex;
  gap: 8px;
}

/* 空状态 */
.empty-categories {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-text {
  font-size: 14px;
  color: #999;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .settings-container {
    padding: 16px;
	    overflow: auto;
  }
  
  .header-content {
    flex-direction: column;
    gap: 16px;
  }
  
  .refresh-page-btn {
    align-self: flex-start;
    margin-left: 0;
  }
  
  .form-row {
    flex-direction: column;
    gap: 12px;
  }
  
  .list-header,
  .item-view,
  .item-edit {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  
  .header-col.actions,
  .item-col.actions,
  .edit-actions {
    grid-column: 1;
  }
}
</style>